import 'package:easy_refresh/easy_refresh.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_controller.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_page_counter.dart';
import 'package:pornhub_app/http/api/api.dart';
import 'package:pornhub_app/model/engagement/engagement_model.dart';

class EngagementVerticalListPageController extends BaseRefreshController
    with BaseRefreshPageCounter {
  final int dataType;
  final int meetType;
  final String classifyId;
  final String cityName;

  EngagementVerticalListPageController({
    required this.dataType,
    required this.meetType,
    required this.classifyId,
    required this.cityName,
  });

  final _pageSize = 10;
  List<EngagementModel> engagementList = List<EngagementModel>.of([]);

  @override
  void onInit() {
    onRefresh();
    super.onInit();
  }

  @override
  Future<IndicatorResult> onRefresh() async {
    resetPage();
    List<EngagementModel>? resp;
    if (dataType == 1) {
      resp = await Api.getEngagementList(
        meetType: meetType,
        classifyId: classifyId,
        cityName: cityName,
        page: page,
        pageSize: _pageSize,
      );
    }

    if (resp == null) {
      return IndicatorResult.fail;
    }
    incPage();
    _setList(resp);
    return IndicatorResult.success;
  }

  @override
  Future<IndicatorResult> onLoad() async {
    List<EngagementModel>? resp;
    if (dataType == 1) {
      resp = await Api.getEngagementList(
        meetType: meetType,
        classifyId: classifyId,
        cityName: cityName,
        page: page,
        pageSize: _pageSize,
      );
    }

    if (resp == null) {
      return IndicatorResult.fail;
    }
    _addList(resp);
    if (resp.length < _pageSize) {
      return IndicatorResult.noMore;
    }
    incPage();
    return IndicatorResult.success;
  }

  void _setList(List<EngagementModel>? list) {
    engagementList.clear();
    _addList(list);
  }

  void _addList(List<EngagementModel>? list) {
    if (null != list && list.isNotEmpty) {
      engagementList.addAll(list);
      update([cityName]);
    }
  }
}
